/**
 * Created by XQ on 2015/7/20.
 */
$().ready(function(){

    var $search = QSearch.init(wp.config.baseUrl + "/rest/weibo/list"),
        $footer = QFooter.init("#weiboFooter", function(pageNum){
            $search.setPageNum(pageNum);
            $search.doSearch();
        });

    $search.onSearch(function(result){
        if(result){
            var data = result.data,
                list = result.data.wbList,
                $DOM = $("#weiboList").empty();

            for(var i = 0, len = list.length; i<len; i += 1){
                var $div = $("#row_modul").clone(true).show(),
                    weibo = list[i],
                    $userPhoto = $div.find(".user_short_img");

                if(weibo.user.photo){   //用户头像处理
                    $userPhoto.attr("src", weibo.user.photo);
                    $userPhoto.attr("title", weibo.user.photo);
                } else {
                    $userPhoto.attr("src", wp.config.baseUrl + "/modules/common/images/user_pic01.png");   //TODO 修改默认用头像
                }

                $div.find(".user_name").html(weibo.user.name);  //用户姓名
                $div.find(".user_info span").eq(0).html(weibo.user.university);    //大学
                $div.find(".user_info span").eq(1).html(weibo.user.language);     //语言
                $div.find(".user_info span").eq(2).html(weibo.user.grade);         //年级
                $div.find(".weibo_content").html(weibo.content);

                if(weibo.img_url){
                    $div.find(".weibo_img_wrap").show();
                    $div.find(".weibo_img_wrap img").attr("src", weibo.img_url);
                } else {
                    $div.find(".weibo_img_wrap").hide();
                }

                var tmpDate = new Date(weibo.create_date);
                $div.find(".weibo_time").html(tmpDate.format("yyyy-MM-dd hh:mm:ss TT"));
                $div.find(".weibo_prise").html("赞&nbsp;" + weibo.prise);

                $DOM.append($div);
            }
            $footer.refresh(data.pageNum, data.pageSize, data.total);
        }
    });

    $("#search_btn").bind("click", function() {

        $search.setPageNum(0);
        $search.addCondition("content", $("#content").val());
        $search.doSearch();
     });

    $("#content").keydown(function(event){
        if(event.keyCode == 13) $("#search_btn").click();
    });

    $("#search_btn").click();

    $(".uni_item").each(function(){
        $(this).bind("click", function(){
            $search.addCondition("user.university", $(this).attr("value"));
            $search.doSearch();
            alert($(this).attr("value"));
        });
    });
});